import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Core } from '../Core/Core';
import { EBuff, EEvent } from '../Config/Enum';


const { ccclass, property } = _decorator;

@ccclass('EnemyStatusView')
export class EnemyStatusView extends Component {

    ndBG: Node = null!; // 背景节点
    ndBoard: Node
    ndDebuffs: Node = null!; // debuff节点


    protected onLoad(): void {
        this.ndBG = this.node.getChildByName("BG");

        //this.itemType = Core.Instance.currentItem.itemType

    }

    protected onEnable(): void {


        // 事件
        this.ndBG.on(Node.EventType.TOUCH_END, this.onClickCloseWindow, this);
        Core.Instance.event.on(EEvent.EnemyBuffChanged, this.onEnemyBuffChanged, this);



    }

    protected onDisable(): void {
        this.ndBG.off(Node.EventType.TOUCH_END, this.onClickCloseWindow, this);
        Core.Instance.event.off(EEvent.EnemyBuffChanged, this.onEnemyBuffChanged, this);

        
    }

    start() {



        


    }

    update(deltaTime: number) {
        
    }

    



    // 获取词缀描述
    private getBuffDescription(type: EBuff, value: number, isPercentage?: boolean): string {
        const formattedValue = isPercentage ? 
            `${(value * 100).toFixed(1)}%` : 
            value.toFixed(1);
        
        switch (type) {
            case EBuff.Slow: return `减速${formattedValue} `;
            case EBuff.Stun: return `晕眩`;
            case EBuff.Vulnerable: return `易伤`;
            case EBuff.Poison: return `中毒${formattedValue} `;
            case EBuff.KnockBack: return `击退`;
            case EBuff.Weak: return `虚弱`;
            case EBuff.Fear: return `恐惧`;
            case EBuff.Silence: return `沉默`;
            case EBuff.Burn: return `燃烧${formattedValue}`;
            case EBuff.DefenceDown: return `减防御${formattedValue}`;
            case EBuff.AttackDown: return `减攻击${formattedValue}`;
            case EBuff.Blind: return `致盲`;
            case EBuff.Explosion: return `毒爆`;
            case EBuff.Freeze: return `冰冻`;
            case EBuff.Miss: return `命中下降${formattedValue}`;
            case EBuff.Disarm: return `缴械`;




            // case EAffixType.SkillDamage: return `+${formattedValue} 武学伤害`;
            // case EAffixType.Speed: return `+${formattedValue} 移动速度`;
            // case EAffixType.ElementalDamage: return `+${formattedValue} 元素伤害`;
            default: return "未知属性";
        }
    }

    onClickCloseWindow() {
        console.log("点击了BG");
        // 关闭详情
        this.node.active = false;



    }

    onEnemyBuffChanged() {
        // 更新敌人状态
        this.ndDebuffs = this.node.getChildByName("Board").getChildByName("Debuffs");
        this.ndDebuffs.removeAllChildren(); // 清空当前的debuff列表

        // 获取敌人所有的debuff
        let debuffs = Core.Instance.currentEnemy.buffManager.buffs

        // 遍历并创建debuff节点
        debuffs.forEach((buff,index) => {
            let buffNode = new Node(buff.type);
            // 设置位置（垂直排列）
            //buffNode.setPosition(0, -index * 30, 0);
            let label = buffNode.addComponent(Label);
            label.fontSize = 15; // 设置较小的字体
            label.lineHeight = 15; // 设置行高
            //label.string = `${buff.type}: ${buff.ap} AP, Duration: ${buff.duration}s`;
            console.log("buff.type, buff.ap, buff.isPercentage:",buff.type, buff.ap, buff.isPercentage)
            label.string = this.getBuffDescription(buff.type, buff.ap, buff.isPercentage);
            this.ndDebuffs.addChild(buffNode);
        });
    }




}


